package kr.or.kead.ui.insert_update;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import kr.or.kead.domain.Depart;
import kr.or.kead.domain.InfoStudent;
import kr.or.kead.module.EmailCheck;
import kr.or.kead.module.JuminCheck;
import kr.or.kead.module.PostGetaddr;
import kr.or.kead.module.RegDate;
import kr.or.kead.module.TelCheck;
import kr.or.kead.service.DaoHandicap;
import kr.or.kead.service.DaoInfoStudent;
import kr.or.kead.service.Daodepart;
import kr.or.kead.ui.meun.MenuMgn;

public class StdInsertUpdate extends AbsInsertUpdate {


	private JTextField txtStdname;
	private JuminCheck txtJumin;
	private RegDate txtStart;
	private RegDate txtEnd;
	private TelCheck txtMobile;
	private TelCheck txtTel;
	private JTextField txtAddr;
	private JTextField txtRoom;
	private JComboBox<Object> comboStdType;
	private JComboBox<Integer> comboGrade;
	private EmailCheck txtEmail;

	private MenuMgn menuMgn;
	private JLabel lblNewLabel;
	private JComboBox comboDepart;
	private Daodepart daoDepart;
	private DaoHandicap daohandi;
	
	static final Integer[] GRADE={1,2,3,4,5,6};
	
	public StdInsertUpdate(Object obj) {
		super(obj, "학사정보");
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void initDao() {
		daotable=new DaoInfoStudent();
		daoDepart = new Daodepart();
		daohandi = new DaoHandicap();
	}

	@Override
	protected boolean isValidCheck() {
		boolean isField =txtStdname.getText().equals("")||txtAddr.getText().equals("")||txtRoom.getText().equals("")||txtEmail.getEmail().equals("");
		if (isField) {
			JOptionPane.showMessageDialog(null, "비어있거나 형식이 잘못된 란이 있습	니다");
			txtStdname.requestFocus();
			return false;
		}
		return true;
	}

	@Override	
	protected JPanel getMainPanel() {
		JPanel centerP = new JPanel();
		centerP.setLayout(new GridLayout(12, 2));
		JLabel lblName = new JLabel("성      명");
		lblName.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblName);
		
		txtStdname = new JTextField();
		centerP.add(txtStdname);
		txtStdname.setColumns(10);
		
		JLabel lblJuminnum = new JLabel("주민번호");
		lblJuminnum.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblJuminnum);
		
		txtJumin = new JuminCheck();
		centerP.add(txtJumin);
		
		JLabel lblStart = new JLabel("입학날짜");
		lblStart.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblStart);
		
		txtStart = new RegDate();
		//현재날짜
		GregorianCalendar now = new GregorianCalendar();
		txtStart.setTxtDate(now.get(Calendar.YEAR), now.get(Calendar.MONTH)+1, now.get(Calendar.DATE));
		centerP.add(txtStart);
		
		JLabel lblEnd = new JLabel("수료날짜");
		lblEnd.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblEnd);
		
		txtEnd = new RegDate();
		GregorianCalendar endDate = 
				new GregorianCalendar(txtStart.getYear()+1, txtStart.getMonth(), txtStart.getDay());
		endDate.add(Calendar.DATE, -1);
		txtEnd.setTxtDate(endDate.get(Calendar.YEAR), endDate.get(Calendar.MONTH), endDate.get(Calendar.DATE));
		centerP.add(txtEnd);
		
		JLabel lblMobile = new JLabel("휴 대 폰");
		lblMobile.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblMobile);
		
		txtMobile = new TelCheck(true);
		centerP.add(txtMobile);
		
		JLabel lblTel = new JLabel("집 전 화");
		lblTel.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblTel);
		
		txtTel = new TelCheck(false);
		centerP.add(txtTel);
		
		JLabel lblAddr = new JLabel("주      소");
		lblAddr.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblAddr);
		
		txtAddr = new JTextField();
		txtAddr.setText("클릭하세요");
		txtAddr.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				super.mouseClicked(arg0);
				PostGetaddr pga = new PostGetaddr(txtAddr);
				pga.setVisible(true);
			}
			
		});
		centerP.add(txtAddr);
		txtAddr.setColumns(10);
		
		JLabel lblRoom = new JLabel("통학 및 기숙사");
		lblRoom.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblRoom);
		
		txtRoom = new JTextField();
		centerP.add(txtRoom);
		txtRoom.setColumns(10);
		
		JLabel lblStdType = new JLabel("장애  유형");
		lblStdType.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblStdType);
		
		daohandi = new DaoHandicap();
		ArrayList<Object> handis = daohandi.selectDao();
		Object[] arhandis = handis.toArray();
		comboStdType = new JComboBox<>(arhandis);
		centerP.add(comboStdType);
		
		JLabel lblGrade = new JLabel("장애  등급");
		lblGrade.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblGrade);
		
		comboGrade = new JComboBox<>(GRADE);
		centerP.add(comboGrade);
		
		JLabel lblEmail = new JLabel("메       일");
		lblEmail.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblEmail);
		
		txtEmail = new EmailCheck();
		centerP.add(txtEmail);
				
		lblNewLabel = new JLabel("학    과");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		centerP.add(lblNewLabel);
		
		
		Daodepart daoDepart = new Daodepart();
		ArrayList<Object> departs= daoDepart.selectDao();
		int len= departs.size();
		Depart depart;
		Object[] obj = new Object[len];
		for(int i=0; i<len; i++){
			depart = (Depart)departs.get(i);
			obj[i] = depart.getName();
		}
		
		comboDepart = new JComboBox<>(obj);
		centerP.add(comboDepart);
		return centerP;
	}

	@Override
	protected Object getObject() {
		if(obj==null){//추가
			GregorianCalendar startCal = 
					new GregorianCalendar(txtStart.getYear(), txtStart.getMonth(), txtStart.getDay());
			GregorianCalendar endCal = 
					new GregorianCalendar(txtEnd.getYear(), txtEnd.getMonth(), txtEnd.getDay());
			
			InfoStudent std = new InfoStudent(
					txtStdname.getText(), 
					txtJumin.getJumin(), 
					startCal.getTime(), 
					endCal.getTime(), 
					txtMobile.getphone(), 
					txtTel.getphone(), 
					txtAddr.getText(), 
					Integer.parseInt(txtRoom.getText()), 
					daohandi.selectCodeHandicapByName((String)comboStdType.getSelectedItem()), 
					comboGrade.getSelectedIndex()+1, 
					txtEmail.getEmail(),
					daoDepart.selectCodeDepartByName((String)comboDepart.getSelectedItem()));
			return std;
		}else{ //수정
			InfoStudent std = (InfoStudent) obj;
			std.setStdName(txtStdname.getText());
			std.setJuminNum(txtJumin.getJumin());
			
			GregorianCalendar startCal = 
					new GregorianCalendar(txtStart.getYear(), txtStart.getMonth(), txtStart.getDay());
			
			std.setStartDate(startCal.getTime());
			
			GregorianCalendar endCal = 
					new GregorianCalendar(txtEnd.getYear(),txtEnd.getMonth(), txtEnd.getDay());
			std.setEndDate(endCal.getTime());
			
			std.setMobile(txtMobile.getphone());
			std.setTel(txtTel.getphone()); 
			std.setStdAddr(txtAddr.getText());
			std.setRoomNum(Integer.parseInt(txtRoom.getText()));
			
			std.setStdType(daohandi.selectCodeHandicapByName((String)comboStdType.getSelectedItem()));
			std.setGrade(comboGrade.getSelectedIndex()+1);
			std.setEmail(txtEmail.getEmail());
			return std;
		}
	}

	@Override
	protected void updateInit() {
		InfoStudent std = (InfoStudent) obj;
		txtStdname.setText(std.getStdName());
		txtJumin.setjumin(std.getJuminNum());
		StringTokenizer start_st = new StringTokenizer(String.valueOf(std.getStartDate()), "-");
		txtStart.setTxtDate(
				Integer.parseInt(start_st.nextToken()),
				Integer.parseInt(start_st.nextToken()),
				Integer.parseInt(start_st.nextToken()));  
		StringTokenizer end_st = new StringTokenizer(String.valueOf(std.getEndDate()), "-");
		txtEnd.setTxtDate(
				Integer.parseInt(end_st.nextToken()), 
				Integer.parseInt(end_st.nextToken()), 
				Integer.parseInt(end_st.nextToken()));
		txtMobile.setphone(std.getMobile());
		txtTel.setphone(std.getTel());
		txtAddr.setText(std.getStdAddr());
		txtRoom.setText(String.valueOf(std.getRoomNum()));
		comboStdType.setSelectedItem(daohandi.selectTableById(std.getStdType()));
		comboGrade.setSelectedIndex(std.getGrade()-1);
		txtEmail.setEmail(std.getEmail());
		Depart depart = (Depart)daoDepart.selectTableById(std.getDepart());
		comboDepart.setSelectedItem(depart.getName());		

	}

}
